package com.xyd.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AdminFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String path = request.getServletPath();
        HttpSession session = request.getSession(false);
        if ( "/manage/adminLogin.jsp".equals(path) || "/manage/".equals(path) || "/manage/manageLogin".equals(path) ||
                (session != null && session.getAttribute("admin") !=null)){
            filterChain.doFilter(request,response);
        } else {
            response.sendRedirect(request.getContextPath() + "/manage/adminLogin.jsp");
        }

    }

    @Override
    public void destroy() {

    }
}
